# -*- coding: utf-8 -*-
"""fetch the popular commentary from douban.com
"""
from doubanApi import DoubanAPI
import re

testsuit = [u'Naruto_火影忍者', \
            u'黑客帝国动画版[中文字幕]',\
            u'[1995年票房第4]风中奇缘I（附II）（迪士尼动画片）（帝国出品）',\
            u'[241]失去的周末[1945]',\
            u'[潜行者].Stalker.1979',\
            u'狂蟒之灾3DVD中英双字',\
            u'[008]卡萨布兰卡[1942]',\
            u'2008-03-07-史前一万年',\
            u'[TBF][天下第二.The.World.Second.2007][DVDRip-RMVB][国语]',\
            u'[TBF][神奇四侠专辑.Fantastic.4]',\
            u'War.Inc.LiMiTED.DVDRip.XviD-ARiSCO.战争公司',\
            u'1995.【Whisper of the Heart】侧耳倾听[近藤喜文]',\
            u'[超人前传][第八季][avi]',\
            u'2008-01-11-遗愿清单',\
            u'[CNXP][玩具总动员专辑.Toy.Story]',\
            u'[黑客帝国专辑.The.Matrix]',\
            u'[CNXP][极限特工专辑.xXx]',\
            u'[TBF][黑社会.Election.2005][DVDRip-RMVB][粤语]'
            ]
test0 = testsuit[0]

def fetchComment(origString, split = True):
    if split is True:
        name = splitter(origString)
    else:
        name = origString
    dou = DoubanAPI()
    ids = dou.searchMovie(name)
    if ids is None: return (None, None)
    movieInfo = dou.findMovieByID(ids[0])
    comment = dou.findCommentByID(ids[0])
    return (movieInfo, comment)

#pre parse
def splitter(origString):
    ret = []
    splitter = u'[\w+【】（）&\[\]\-\.]'
    ss = re.split(splitter, origString)
    for i in ss:
        if not i: continue
        ret+=[i]
    print ret[0].encode('gb18030')
    return ret[0]


if __name__ == '__main__':
    movieInfo, comment = fetchComment(u'幸福终点站')
    print movieInfo
    print comment
#    star, title, content = fetchComment(testsuit[1])
#    print type(title)
#    print type(content)
#    for i in testsuit:
#        fetchComment(i)
